import request from '@/utils/request'
import { defineStore } from 'pinia'
import type { shopCartData } from '../../api/shopcart'
import shopApi from '../../api/shopcart'


interface stateData {
  shopcart: shopCartData[]
  token: string
}

export const useShopCartStore = defineStore('shopCart', {
  state(): stateData {
    return {
      shopcart: [
        // {
        //     "id": 18482,
        //     "userId": "99c40525-37ab-4dcf-a653-68442710c948",
        //     "skuId": 5,
        //     "cartPrice": 999,
        //     "skuNum": 1,
        //     "imgUrl": "http://47.93.148.192:8080/group1/M00/00/01/rBHu8l-rgJqAHPnoAAF9hoDNfsc505.jpg",
        //     "skuName": "Redmi 10X 4G Helio G85游戏芯 4GB+128GB 明月灰 游戏智能手机 小米 红米",
        //     "isChecked": 1,
        //     "createTime": "2023-03-02 15:38:42",
        //     "operateTime": "2023-03-02 15:38:42",
        //     "isOrdered": 0,
        //     "orderTime": null,
        //     "sourceType": "QUERY",
        //     "sourceId": 5,
        //     "skuPrice": 999,
        //     "couponInfoList": null
        // },
        // {
        //     "id": 18442,
        //     "userId": "99c40525-37ab-4dcf-a653-68442710c948",
        //     "skuId": 13,
        //     "cartPrice": 4188,
        //     "skuNum": 3,
        //     "imgUrl": "http://47.93.148.192:8080/group1/M00/00/02/rBHu8l-sklaALrngAAHGDqdpFtU741.jpg",
        //     "skuName": "华为 HUAWEI P40 麒麟990 5G SoC芯片  6GB+128GB亮黑色全网通5G手机",
        //     "isChecked": 1,
        //     "createTime": "2023-03-02 15:14:30",
        //     "operateTime": "2023-04-05 17:14:57",
        //     "isOrdered": 0,
        //     "orderTime": null,
        //     "sourceType": "QUERY",
        //     "sourceId": 13,
        //     "skuPrice": 4188,
        //     "couponInfoList": null
        // }
      ],
      token: ''
    }
  },

  actions: {
    // 获取数据列表
    async getshopcart() {
      try {
        const result = await shopApi.getShopList()
        this.shopcart = result.data[0].cartInfoList
        // console.log(this.shopcart);
      } catch (error) {
      }
    },

    // 获取token
    async getUserToken() {
      try {
        const result = await shopApi.getToken({
          phone: '18792809667',
          password: 'chen123560'
        })
        // console.log(result);
        this.token = (result.data as any).token
        // console.log(this.token);

        localStorage.setItem('token_key', this.token)
      } catch (error) {
      }
    },

    // 加入购物车
    async addShop(skuId: number, skuNum: number) {
      try {
        const result = await shopApi.addShopCart(skuId, skuNum)

      } catch (error) {
      }
    },

    // 删除单个商品
    async delete(skuId: string) {
      try {
        // console.log(skuId)
        const res = await shopApi.deleteOne(skuId)
        // console.log(res)
        await this.getshopcart()
      } catch (error) {

      }
    },

    // 切换商品选中状态
    async checkStatu(skuId: number, isChecked: number) {
      try {
        await shopApi.checkOne(skuId, isChecked)
      } catch (error) {
      }
    }


  },
  getters: {}
})
